<template>
  <!-- 病历信息 -->
  <section>
    <br>
    <el-form @submit.native.prevent="onSubmit" label-width="100px" ref="form" label-position="top" :model="form" :rules="rules">
      <div style="padding-bottom:10px;">
        <strong>过敏史：</strong>
        &nbsp;&nbsp;&nbsp;
        <el-radio-group v-model="allergystatus">
          <el-radio :label="1">有</el-radio>
          <el-radio :label="0">无</el-radio>
        </el-radio-group>
      </div>
      <!-- <table v-if="allergystatus==1" border="0" width="100%">
        <tr height="45" v-for="(item,index) in form.patientAllergyArr" :key="index">
          <td>
            <el-input v-model="item.allergens" placeholder="过敏源"></el-input>
          </td>
          <td>
            <el-input v-model="item.allergicReaction" placeholder="过敏反应"></el-input>
          </td>
          <td>
            <el-input v-model="item.allergicDegree" placeholder="过敏程度"></el-input>
          </td>
          <td width="65">
            <el-link :underline="false" @click="onDel(index)"> <i class="el-icon-remove" style="font-size:30px;"></i></el-link>
            <el-link v-if="index==form.patientAllergyArr.length-1" :underline="false" @click="onAdd(index)"> <i class="el-icon-circle-plus" style="font-size:30px;"></i></el-link>
          </td>
        </tr>
      </table> -->
      <guoMinShi v-if="allergystatus==1" v-model="formGuoMin.patientAllergyArr" :isAdd="true" @onAdd="onAddGuoMin"></guoMinShi>
      <div>
        <el-form-item label="个人史：" prop="personalHistory">
          <el-input v-model="form.personalHistory" type="textarea" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="家庭史：" prop="familyHistory">
          <el-input v-model="form.familyHistory" type="textarea" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="既往史：" prop="pastHistory">
          <el-input v-model="form.pastHistory" type="textarea" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="经带胎产史：" prop="afterTheBirth">
          <el-input v-model="form.afterTheBirth" type="textarea" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="疫苗接种史：" prop="seedlingsHistory">
          <el-input v-model="form.seedlingsHistory" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="">
        <el-button type="primary" :loading="btnLoading" native-type="submit">提交</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script>
export default {
  props: ["patientId"],
  components: {
    guoMinShi: () => import("@/pages/_com/guoMinShi.vue")
  },
  data() {
    return {
      btnLoading: false,
      allergystatus: 0,
      form: {
        patientId: "",
        historyId: "",
        pastHistory: "",
        personalHistory: "",
        familyHistory: "",
        seedlingsHistory: "",
        afterTheBirth: "",
        isDeleted: 0
      },
      formGuoMin: {
        patientAllergy: "",
        patientAllergyArr: []
      },
      rules: {}
    };
  },
  mounted() {
    let th = this;
    th.form.patientId = th.patientId;
    th.getData();
  },
  methods: {
    async onSubmit() {
      let th = this;
      let j = {};
      let fn = async function() {
        th.btnLoading = true;
        if (th.allergystatus == 1) {
          j = {};
          Object.assign(j, th.formGuoMin);
          delete j.patientAllergyArr;
          j.patientAllergy = JSON.stringify(th.formGuoMin.patientAllergyArr);
          let res1 = await th.$api.saveAllergyAndHistory(j);
        }
        j = {};
        Object.assign(j, th.form);
        let res2 = await th.$api.patientHistorySaveHistory(j);
        th.$message.success("操作成功");
        th.btnLoading = false;
        th.$emit("success");
      };
      th.$refs["form"].validate(v => {
        if (v) {
          fn();
        }
      });
    },
    onAddGuoMin() {
      let th = this;
      let j = {
        allergens: "",
        allergicReaction: "",
        allergicDegree: "",
        patientId: th.form.patientId
      };
      th.formGuoMin.patientAllergyArr.push(j);
    },
    onDel(index) {
      let th = this;
      if (index == 0) return;
      th.formGuoMin.patientAllergyArr.splice(index, 1);
    },
    onAdd(index) {
      let th = this;
    },
    getData() {
      let th = this;
      let j = {
        patientId: th.form.patientId
      };
      //过敏
      th.$api.getAllergyById(j).then(res => {
        if (res.executed) {
          th.$utils.joinFormJson(th.formGuoMin, res.patientAllergy);
          if (res.patientAllergyList.length > 0) {
            th.formGuoMin.patientAllergyArr = res.patientAllergyList;
             th.allergystatus = 1;
          } else {
            th.formGuoMin.patientAllergyArr.push({
              allergyId: "",
              patientId: th.form.patientId,
              allergens: "",
              allergicReaction: "",
              allergicDegree: ""
            });
          }
        }
      });
      //病案
      th.$api.getPatientHistoryById(j).then(res => {
        if (res.executed) {
          th.$utils.joinFormJson(th.form, res.patientHistory);
        }
      });
    },
    onReset() {
      const th = this;
      th.allergystatus = 0;
      th.formGuoMin.patientAllergyArr = [];
      th.$refs["form"].resetFields();
    }
  }
};
</script>
